# -*- coding:utf-8 -*-
from fastapi import APIRouter, Request, Depends, Query
from typing import Union
from core.response import tableinfo, success, fail
from core.curd import PageInfo, IDList
from core.request import get_page_info, get_query_param
from app.annotation import RequiresPermissions
from app.service.system import SysDictTypeService, SysDictDataService
from app.entity.system import CreateSysDictType, CreateSysDictData, UpdateSysDictData, UpdateSysDictType

router = APIRouter()


@router.get('/type/get')
@RequiresPermissions('sys:dict:type:detail')
async def type_get(req: Request, ids: int = Query(), mService=Depends(SysDictTypeService.instance)):
    """
        获取测试详情
    """
    one = await mService.select_by_id(ids)
    return success('OK', one)


@router.get("/type/all")
@RequiresPermissions('sys:dict:type:all')
async def type_all(req: Request, mService=Depends(SysDictTypeService.instance)):
    ret = await mService.get_all_dict_type()
    return success("ok!", ret)


@router.get("/type/list")
@RequiresPermissions('sys:dict:type:list')
async def type_list(req: Request, page=Depends(get_page_info), where=Depends(get_query_param),
               mService=Depends(SysDictTypeService.instance)):
    """
        获取测试列表
    """
    total, rows = await mService.select_page(page, where)
    return tableinfo('OK', total, rows)


@router.get('/type/sql')
@RequiresPermissions('sys:dict:type:sql')
async def type_sql(req: Request, page=Depends(get_page_info), where=Depends(get_query_param),
               mService=Depends(SysDictTypeService.instance)):
    """
        添加测试
    """
    one = await mService.select_by_query(where)
    if one is None or one.dict_source == 0:
        return tableinfo('OK')

    total, rows = await mService.get_sql_data(one.dict_sql)

    return tableinfo('OK', total, rows)


@router.post('/type/add')
@RequiresPermissions('sys:dict:type:add')
async def type_add(post: CreateSysDictType, mService=Depends(SysDictTypeService.instance)):
    """
        添加测试
    """
    one = await mService.insert(post.dict())
    return success('OK', one)


@router.post('/type/edit')
@RequiresPermissions('sys:dict:type:edit')
async def type_update(post: UpdateSysDictType, mService=Depends(SysDictTypeService.instance)):
    """
        编辑测试
    """
    one = await mService.update(post.dict())
    return success('OK', one)


@router.post('/type/del')
@RequiresPermissions('sys:dict:type:remove')
async def type_remove(ids: IDList, mService=Depends(SysDictTypeService.instance)):
    """
        删除测试
    """
    await mService.delete_dict_type(ids.ids)

    return success('OK')


@router.get('/data/get')
@RequiresPermissions('sys:dict:data:detail')
async def data_get(ids: int = Query(), mService=Depends(SysDictDataService.instance)):
    """
        获取测试详情
    """
    one = await mService.select_by_id(ids)
    return success('OK', one)


@router.get("/data/list")
@RequiresPermissions('sys:dict:data:list')
async def data_list(req: Request, page=Depends(get_page_info), where=Depends(get_query_param),
               mService=Depends(SysDictDataService.instance)):
    """
        获取测试列表
    """
    total, rows = await mService.select_page(page, where)
    return tableinfo('OK', total, rows)


@router.post('/data/add')
@RequiresPermissions('sys:dict:data:add')
async def data_add(post: CreateSysDictData, mService=Depends(SysDictDataService.instance)):
    """
        添加测试
    """
    one = await mService.insert(post.dict())
    return success('OK', one)


@router.post('/data/edit')
@RequiresPermissions('sys:dict:data:edit')
async def data_update(post: UpdateSysDictData, mService=Depends(SysDictDataService.instance)):
    """
        编辑测试
    """
    one = await mService.update(post.dict())
    return success('OK', one)


@router.post('/data/del')
@RequiresPermissions('sys:dict:data:remove')
async def data_remove(ids: IDList, mService=Depends(SysDictDataService.instance)):
    """
        删除测试
    """
    one = await mService.delete_batch_ids(ids.ids)
    return success('OK')
